﻿using Microsoft.AspNetCore.Mvc;
using SYSoft.Comm;
using SYSoft.Comm.Utils;
using SYSoft.Model;
using SYSoft.Model.Models.DeviceManage.Maintain;
using SYSoft.Services.MaintainManage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft.Controllers.MaintainManage
{
    public class WH_DataController : BaseController
    {
        IWH_BaselibService wh;
        IWH_DataService wh_data;
        public WH_DataController(IWH_DataService wh_data, IWH_BaselibService wh)
        {
            this.wh_data = wh_data;
            this.wh = wh;
        }




        /// <summary>
        /// 根据时间获取当天所有任务  
        /// </summary>
        /// <param name="time">2012-09-21</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<PageModel<WH_Data>>> GetDaliyCheckTasks(string time1, string time2, string devicename, int page, int size)
        {
            DateTime dt1 = Convert.ToDateTime(time1 + " 00:00:00");
            DateTime dt2 = Convert.ToDateTime(time2 + " 23:59:59");
            var model = await wh_data.QueryPage((item => item.CreatTime >= dt1 && item.CreatTime <= dt2 && item.DeviceName == devicename), page, size);
            if (model.data != null && model.data.Count > 0)
            {
                return MessageModel.CreateSuccessResult(model);
            }
            return MessageModel.CreateFailedResult<PageModel<WH_Data>>("");
        }


        /// <summary>
        /// 导出获取基础库所有任务 Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAllWhTasks(string time1, string time2, string devicename)
        {

            DataTable dataTable = null;
            DateTime dt1 = Convert.ToDateTime(time1 + " 00:00:00");
            DateTime dt2 = Convert.ToDateTime(time2 + " 23:59:59");
            var model = await wh_data.Query(item => item.CreatTime >= dt1 && item.CreatTime <= dt2 && item.DeviceName == devicename);
            if (model != null && model.Count > 0)
            {
                dataTable = UtilConvert.ListToDt(model);
                if (dataTable == null || dataTable.Rows.Count < 1)
                    return NotFound("未查询到数据！！");
                string name = "数据详情";
                byte[] bytes = ExcelHelper.DataTableToExcel(dataTable);
                return File(bytes, "application/octet-stream", $"{name}_{DateTime.Now:yyyyMMddHHmmssfff}.xls");
            }
            else
            {
                return NotFound("未查询到数据！！");
            }
        }



        /// <summary>
        /// 导出所有未维护的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAllNofinishWhTasks()
        {
            DataTable dataTable = null;
            var model = await wh_data.Query(item => item.Flag==1);
            if (model != null && model.Count > 0)
            {
                dataTable = UtilConvert.ListToDt(model);
                if (dataTable == null || dataTable.Rows.Count < 1)
                    return NotFound("未查询到数据！！");
                string name = "数据详情";
                byte[] bytes = ExcelHelper.DataTableToExcel(dataTable);
                return File(bytes, "application/octet-stream", $"{name}_{DateTime.Now:yyyyMMddHHmmssfff}.xls");
            }
            else
            {
                return NotFound("未查询到数据！！");
            }
        }



        /// <summary>
        /// 完成维护
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public  MessageModel FinishWh(int id, int libid, string name, string whbeizhu, string whplantime)
        {
            if (string.IsNullOrEmpty(whbeizhu))
                whbeizhu = "";
            if(Convert.ToDateTime( whplantime)<DateTime.Now.AddMinutes(1))
                return new MessageModel("计划时间必须大于当前时间");
            MysqlHelper mysql = new MysqlHelper();
            List<string> sqls = new List<string>();

            string sql = $"update wh_tasks_baselib set Flag={0},PlanTime='{whplantime}' where Id ={libid}";
            string sql1 = $"update wh_tasks_data set Flag={2},PeopleName='{name}',BeiZhu='{whbeizhu}',FinishTime='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' where Id ={id}";
            sqls.Add(sql);
            sqls.Add(sql1);
            bool flag = mysql.ExecuteCommandByTran(sqls);
            if (flag)
                return MessageModel.CreateSuccessResult();
            else
                return new MessageModel("修改失败");
        }


        /// <summary>
        /// 消除维护（没有实际完成）
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public MessageModel NoFinishWh(int id, int libid, string name)
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql = $"update wh_tasks_baselib set Flag={2} where Id ={libid}";
            int t = mysql.ExecuteCommand(sql);
            if (t>0)
                return MessageModel.CreateSuccessResult();
            else
                return new MessageModel("修改失败");
        }

    }
}
